package com.datastructure.test.findrepeatnum;

import java.util.*;
import java.util.stream.Collectors;

public class FindRepeatNum {

    public static void main(String[] args) {

    }

    public int findRepeatNum (ArrayList<Integer> nums) {
        // write code here
        if(nums==null){
            return -1;
        }
        List<Integer> collect = nums.stream().sorted().collect(Collectors.toList());
        for (int i = 0; i < collect.size()-1; i++) {
            if(collect.get(i).equals(collect.get(i + 1))){
                return collect.get(i);
            }
        }
        return -1;
    }

    public int reArrangeArray (int[] nums) {
        // write code here
        Set<Integer> set = new HashSet<>();
        ArrayList<Integer> list = new ArrayList<>();
        for (int num : nums) {
            if(!set.contains(num)){
                set.add(num);
            } else {
                list.add(num);
            }
        }
        int index = 0;
        for (Integer integer : set) {
            nums[index] = integer;
            index++;
        }
        Arrays.sort(nums);
        for (Integer integer : list) {
            nums[index]=integer;
            index++;
        }

        return set.size();
    }
}
